package com.bwie.parkdemo.dao;

import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;

import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;

/**
 * 用户增删改查工具类
 * 根据需要持久化存储部分设计对应方法
 * */
public class LoginUserServe {

    private DaoHelperUtils utils;
    private Context ctx;

    public LoginUserServe(Context ctx){
        this.ctx = ctx;
        utils = new DaoHelperUtils(ctx,null,null,1);
    }

    //添加用户缓存
    public int insertUser(LoginEntity.Values entity){
        int index = 0;//添加结果
        if (entity == null)
            return index;
        if (entity.pId <= 0)
            return index;
        if (entity.uId <=0)
            return index;
        if (TextUtils.isEmpty(entity.uName))
            return index;
        if (TextUtils.isEmpty(entity.uPwd))
            return index;
        if (TextUtils.isEmpty(entity.token))
            return index;
        if (TextUtils.isEmpty(entity.time))
            return index;
        ContentValues values = new ContentValues();
        values.put("uId",entity.uId);
        values.put("pId",entity.pId);
        values.put("uName",entity.uName);
        values.put("uPwd",entity.uPwd);
        values.put("token",entity.token);
        values.put("time",entity.time);
        index = utils.insert("sys_user",values);
        return index;
    }

    //删除用户缓存
    public int delUser(int uId){
        int index = 0;
        index = utils.del("sys_user","uId=?",new String[]{""+uId});
        return index;
    }
    //查询用户缓存
    public LoginEntity.Values selUser(int uId){
        LoginEntity.Values entity;
        entity = utils.selUser("select * from sys_user where uId = "+uId);
        return entity;
    }
    //查询用户缓存
    public LoginEntity.Values selUser(){
        LoginEntity.Values entity;
        entity = utils.selUser("select * from sys_user ");
        return entity;
    }
}
